package listerner;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;

import myaddressbook.AddUser;
/**
 * 修改功能监听器
 * @author Little-Grey
 *
 */
public class ModifyListerner  implements ActionListener{
	JTextField Tname;
	JDialog dialog;
	public ModifyListerner(JTextField Tname,JDialog dialog) {
		// TODO Auto-generated constructor stub
		this.Tname = Tname;
		this.dialog = dialog;
	}
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		try {
			File file=new File("D:\\AddressBook.txt");
			Scanner find=new Scanner(file);
			FileWriter file1=new FileWriter("D:\\AddressBook1.txt",true);
			PrintWriter write=new PrintWriter(file1);
			while(find.hasNext()){
				String s=find.next();
			if(!(s.equals(Tname.getText()))){
				write.print(s+'\t');
				write.print(find.next()+'\t');
				write.print(find.next()+'\t');
				write.print(find.next()+' ');
				write.print(find.next()+' ');
				write.print(find.next()+'\t');
				write.print(find.next()+'\t');
				write.println(find.next());
			}
			else{
				dialog.dispose(); 
				AddUser fd=new AddUser();
					AddUser.y=1;
				fd.frame=new JFrame("查询结果如下");
				fd.name.setText(Tname.getText());
				String sex = find.next();
				fd.Csex.select(sex);
				String s2=find.next();
				fd.Cbirthyear.select(s2);
				String s3=find.next();
				fd.Cbirthmonth.select(s3);
				String s4=find.next();
				fd.Cbirthday.select(s4);
				String s5=find.next();
				fd.phone.setText(s5);
				String s6=find.next();
				fd.email.setText(s6);
				String s7=find.next();
				fd.other.setText(s7);
				fd.addUser();
				if(AddUser.z==1){
					write.print(Tname.getText()+'\t');
					write.print(sex+'\t');
					write.print(s2+' ');
					write.print(s3+' ');
					write.print(s4+'\t');
					write.print(s5+'\t');
					write.print(s6+'\t');
					write.println(s7);
					AddUser.z=2;		
					}
				}
			}
			find.close();
			file.delete();
			write.close();
			file1.close();
			File file2=new File("D:\\AddressBook1.txt");
			file2.renameTo(new File( "D:\\AddressBook.txt"));	
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			System.out.print("未找到文件");
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			System.out.print("未找到该人员");
		} 
	}
}
